from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import typejson
import json


class Handler(BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        BaseHTTPRequestHandler.__init__(self, request, client_address, server)

    def do_POST(self):
        s = self.rfile.read(int(self.headers['content-length']))

        try:
            res = typejson.loads(s)
        except typejson.TypeJsonParseError, e:
            self.send_response(200)
            self.end_headers()
            self.wfile.write(json.dumps({
                'status': 'error',
                'message': {
                    'compile_process': e.compile_process,
                    'msg': e.msg,
                    'line_col': {
                        'line': e.line_col.line,
                        'col': e.line_col.col
                    }
                }
            }))
            return

        self.send_response(200)
        self.end_headers()
        self.wfile.write(json.dumps({'status': 'success', 'message': res}))


http_server = HTTPServer(('127.0.0.1', 60000), Handler)
http_server.serve_forever()
